//
//  WYCTableCell.m
//  WYCweibo
//
//  Created by 魏云超 on 15/11/25.
//  Copyright © 2015年 魏云超. All rights reserved.
//

#import "WYCTableCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "WYCStatus.h"
#import "WYCUser.h"
#import "WYCPhoto.h"
#import "WYCPictureCell.h"
@interface WYCTableCell()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *sourceLabel;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UIImageView *VIPView;

@property (weak, nonatomic) IBOutlet UIView *contentView;
//配图高度
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *photoViewHeight;
//配图宽度
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *photoViewWidth;
//底部工具条
@property (weak, nonatomic) IBOutlet UIView *bottomToolView;

@end

@implementation WYCTableCell


+(NSString *)identifier
{
    return @"cell";
}
- (void)awakeFromNib {
    // Initialization code
    //之加载一次
    //设置正文的宽度
    self.contentLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setStatus:(WYCStatus *)status
{
    _status = status;
    WYCUser *user = _status.user;
    NSURL *url = [NSURL URLWithString:user.profile_image_url];
    [self.iconView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"abc"]];
    //昵称
    self.nameLabel.text = user.name;
    //会员图标
    
    //时间
    self.timeLabel.text = _status.created_at;
    //来源
    self.sourceLabel.text = _status.source;
    //正文
    self.contentLabel.text = _status.text;
    //1.计算配图宽高
    CGSize photoSize = [self caclPhotoSize];
    self.photoViewHeight.constant = photoSize.height;
    self.photoViewWidth.constant = photoSize.width;
//    CGSize temp = self.contentView.size;
//    temp.height += photoSize.height;
//    self.contentView.size = temp;
}
-(CGSize)caclPhotoSize
{
    NSUInteger count = self.status.pic_urls.count;
    if (count == 0) {
        return CGSizeZero;
    }
    //列数
    NSUInteger maxCol = count == 4 ? 2 : 3;
    
    NSUInteger col =count > 3 ? maxCol : count;
    //行数
    NSUInteger row = 1;
    if (count % 3 == 0) {
        row = count / 3;
    }else
    {
        row = count /3 +1;
    }
    //计算宽高
    CGFloat pictureHeight = 90;
    CGFloat pictureWidth = 90;
    CGFloat pictureMargin = 10;
    //宽度= 列数*配图宽度+(列数-1)*间隙;
    pictureWidth = col * 90 + (col - 1)*pictureMargin;
    //高度= 行数*配图宽度+(行数-1)*间隙;
    pictureHeight = row * 90 + (row - 1)*pictureMargin;
    
   
    return  CGSizeMake(pictureWidth, pictureHeight);
}
#pragma mark - UICollectionViewDataSource

//每组有多少item
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.status.pic_urls.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //1.创建图片cell
    WYCPictureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[WYCPictureCell identifier] forIndexPath:indexPath];
    //2.获取微博模型
    WYCPhoto *photo = self.status.pic_urls[indexPath.row];
    //3.设置上数据
    cell.photo = photo;
    return cell;
                                   
}


#pragma mark - 外部方法

-(CGFloat)cellHeightWithStatus:(WYCStatus *)status
{
    self.status = status;
    //强制更新布局
    [self layoutIfNeeded];
    return CGRectGetMaxY(self.bottomToolView.frame);
}

@end






























